package com.ztt.controller.user;

import com.ztt.pojo.entity.zttUser;
import com.ztt.result.Result;
import com.ztt.utils.MD5;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ztt.service.UserService;

@RequestMapping("/user")
@RestController
public class UserController {

    @Autowired
    private UserService userService;

    @ApiOperation("用户注册")
    @PostMapping("/register")
    public Result<String> register(@RequestBody zttUser user) {
        zttUser u = userService.getUserByUsername(user.getName());
        String UserName = user.getName();
        String Password = user.getPassword();
        if(UserName.isEmpty() || UserName.length()>20 || Password == null || Password.length()<5 || Password.length()>20){
            return Result.error("用户名不符合规则");
        }
        if (u == null) {
            //没有占用
            //注册
            userService.insertUser(user);
            return Result.success();
        }else {
            return Result.error("用户名已被占用");
        }
    }


    @ApiOperation("用户登录")
    @PostMapping("/login")
    public Result<String> login(@RequestBody zttUser user) {


        String UserName = user.getName();
        String Password = user.getPassword();
        if(UserName.isEmpty() || UserName.length()>20 || Password == null || Password.length()<5 || Password.length()>20){
            return Result.error("用户名不符合规则");
        }
        zttUser u = userService.getUserByUsername(user.getName());
        if(u == null){
            return Result.error("用户名错误");
        }
        if(MD5.md5util(Password).equals(u.getPassword())){
            return Result.success("vbnrjdfkenbajrefdbjn");
        }else
            return Result.error("密码错误");

    }




}
